<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function varTest() {
            var x = 1; {
                var x = 2; // same variable!
                console.log(x); // 2
            }
            console.log(x); // 2
        }

        function letTest() {
            let x = 1; {
                let x = 2; // different variable
                console.log(x); // 2
            }
            console.log(x); // 1
        }
    </script>
</head>

<body>

</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function varTest() {
            var x = 1; {
                var x = 2; // same variable!
                console.log(x); // 2
            }
            console.log(x); // 2
        }

        function letTest() {
            let x = 1; {
                let x = 2; // different variable
                console.log(x); // 2
            }
            console.log(x); // 1
        }
    </script>
</head>

<body>

</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        function varTest() {
            var x = 1; {
                var x = 2; // same variable!
                console.log(x); // 2
            }
            console.log(x); // 2
        }
        varTest()
        /* 
            let修饰的变量有严格的块区分，优先使用块内定义的，并且快于块之间不会相互影响
         */
        function letTest() {
            let x = 1; {
                let x = 2; // different variable
                // let x=1;
                console.log(x); // 2
            }
            console.log(x); // 1
        }
        letTest()
    </script>
</head>

<body>

</body>

</html>